/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.controller.userServlet;

import presentation.controller.userServlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.DAO.FriendListDAO;
import model.entity.FriendList;

/**
 *
 * @author ninkuhack: Qu?n lý friend c?a user, li?t kê các friend c?a user dó,
 * cho phép d?ng ý k?t b?n ho?c xóa friend b?t k? Quang th?c hi?n
 */
@WebServlet(name = "FriendServlet", urlPatterns = {"/FriendServlet"})
public class FriendServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FriendServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FriendServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        FriendListDAO f = FriendListDAO.getInstance();
        FriendList friendList = (FriendList) request.getAttribute("FriendList");
        if (request.getParameter("submit").equals("AcceptFriend")) {
            f.updateFriendList(friendList);
        }
        if (request.getParameter("submit").equals("Unfriend")|| request.getParameter("submit").equals("CancelFriendRequest")) {
            f.deleteFriendList(friendList);
        }
        if (request.getParameter("submit").equals("SendFriendRequest")) {
            f.insertFriendList(friendList);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        FriendListDAO f = FriendListDAO.getInstance();
        FriendList friendList = (FriendList) request.getAttribute("FriendList");
        if (request.getParameter("submit").equals("AcceptFriend")) {
            f.updateFriendList(friendList);
        }
        if (request.getParameter("submit").equals("Unfriend")|| request.getParameter("submit").equals("CancelFriendRequest")) {
            f.deleteFriendList(friendList);
        }
        if (request.getParameter("submit").equals("SendFriendRequest")) {
            f.insertFriendList(friendList);
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
